package com.email.box.utils;

import java.util.List;

public class PageUtil {
    /**
     * 分页插件
     * * @currPageNo  页面传入的页号，从一开始
     * * @pageSize    每页记录数
     */
    public static <T> List<T> getPage(List<T> list, Integer currPageNo, Integer pageSize) {
        if (list.size() == 0) {
            return list;
        }

        int totalRowNum = list.size(); //总记录数
        if (pageSize > list.size()) {  //如果分页大小大于总记录数
            pageSize = list.size();
        }
        int totalPageNum = (totalRowNum + pageSize - 1) / pageSize;  //总页码数
        int realPageNo = currPageNo;      //第几页
        if (currPageNo > totalPageNum) {
            realPageNo = totalPageNum;
        } else if (currPageNo < 1) {
            realPageNo = 1;
        }
        int fromIdx = (realPageNo - 1) * pageSize;  //获取起始索引数
        int toIdx = Math.min(realPageNo * pageSize, totalRowNum); //获取结束索引数

        return list.subList(fromIdx, toIdx);
    }
}
